package dgut.lgc.test;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.context.event.SimpleApplicationEventMulticaster;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import java.util.concurrent.ThreadPoolExecutor;

@EnableConfigurationProperties({MyProperties.class,MyThreadPoolProperties.class})
public class MyAutoConfig {

    @Autowired
    MyThreadPoolProperties poolProperties;

    @Bean
    @ConditionalOnProperty(prefix = "test.auto", name = "enable", havingValue = "true")
    CommandLineRunner createCustomCommandLineRunner(Environment env) {
        return new MyCommandLineRunner(env);
    }

    @Bean
    public ThreadPoolTaskExecutor myTaskPool() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(poolProperties.getCorePoolSize());
        executor.setMaxPoolSize(poolProperties.getMaxPoolSize());
        executor.setQueueCapacity(poolProperties.getQueueCapacity());
        executor.setKeepAliveSeconds(poolProperties.getKeepAliveSeconds());
        executor.setThreadNamePrefix("MyTask-");
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        return executor;
    }

    @Bean(AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME)
    ApplicationEventMulticaster customApplicationEventMulticaster(@Qualifier("myTaskPool") ThreadPoolTaskExecutor taskExecutor) {
        SimpleApplicationEventMulticaster eventMulticaster = new SimpleApplicationEventMulticaster();
        eventMulticaster.setTaskExecutor(taskExecutor);
        return eventMulticaster;
    }

    //自定义事件类
    static public class NoticeEvent extends ApplicationEvent {

        private static final Logger logger = LoggerFactory.getLogger(NoticeEvent.class);

        private final String message;

        public NoticeEvent(String message) {
            super(message);
            this.message = message;
            logger.info("添加事件成功！message: {}",message);
        }

        public String getMessage() {
            return message;
        }
    }

    //自定义事件监听器
    @Component
    static public class NoticeListener implements ApplicationListener<NoticeEvent> {

        private static final Logger logger = LoggerFactory.getLogger(NoticeListener.class);

        @Override
        public void onApplicationEvent(NoticeEvent noticeEvent) {
            logger.info("事件监听器获取到NoticeEvent，睡眠当前线程2秒...");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e){
                e.printStackTrace();
            }
            logger.info("NoticeEvent的message属性是：{}",noticeEvent.getMessage());
        }
    }
}
